﻿<?php

if(!defined('IN_PHPSCUP'))exit('hacking'); 
class SettingApp extends BaseApp
{
    var $type = 'setting';
    function index()
    {
		$setingdata =array();
		$data = array();
		
		if(is_file(ROOT_PATH . '/data/config_'.trim($GLOBALS['Lang']->_lang_tp).'.inc'))
		{
          include(ROOT_PATH . '/data/config_'.trim($GLOBALS['Lang']->_lang_tp).'.inc');
		  foreach($setingdata as $key => $value)
		  {
           $data[$key] = $value;		   
		  }		 
		 
		}
		else
		{
		  $TB_set = &$this->_model('setting'); 	
		  $setingdata = $TB_set->show();

		  foreach($setingdata as $key => $value)
		  {
           $data[$value['keyname']] = $value['value'];		   
		  }
	
		}
        
		if(empty($data))
		{
		  $TB_set = &$this->_model('setting'); 	
		  $setingdata = $TB_set->allshow();

		  foreach($setingdata as $key => $value)
		  {
           $data[$value['keyname']] = $value['value'];		   
		  }
		
		}
		



		!isset($data['waterimg_dir'])?$data['waterimg_dir']='':'';
		!isset($data['site_url'])?$data['site_url']='':'';
		!isset($data['site_title'])?$data['site_title']='':'';		
		!isset($data['site_keywords'])?$data['site_keywords']='':'';		
		!isset($data['site_description'])?$data['site_description']='':'';		
		!isset($data['miibeian'])?$data['miibeian']='':'';
		!isset($data['upfile_path'])?$data['upfile_path']='':'';		
		!isset($data['thumb_width'])?$data['thumb_width']='':'';		
		!isset($data['thumb_height'])?$data['thumb_height']='':'';			
		!isset($data['is_waterimg'])?$data['is_waterimg']='':'';
		!isset($data['max_file_size'])?$data['max_file_size']='':'';		
		!isset($data['upfile_types'])?$data['upfile_types']='':'';		
		!isset($data['enabled_gzip'])?$data['enabled_gzip']='':'';			
		!isset($data['system_url_rewrite'])?$data['system_url_rewrite']='':'';	
		!isset($data['system_url_html_rewrite'])?$data['system_url_html_rewrite']='':'';				
		!isset($data['version_info'])?$data['version_info']='':'';			
		!isset($data['system_open'])?$data['system_open']='':'';
		!isset($data['system_open_reason'])?$data['system_open_reason']='':'';		
		!isset($data['allow_register'])?$data['allow_register']='':'';		
		!isset($data['close_register_reason'])?$data['close_register_reason']='':'';			
		!isset($data['login_verycode'])?$data['login_verycode']='':'';		
		!isset($data['register_verycode'])?$data['register_verycode']='':'';		
		!isset($data['lang'])?$data['lang']='':'';		
		!isset($data['cookie_domain'])?$data['cookie_domain']='':'';		
		!isset($data['cookie_path'])?$data['cookie_path']='':'';		
		!isset($data['cookie_secure'])?$data['cookie_secure']='':'';		
		!isset($data['cookie_prefix'])?$data['cookie_prefix']='':'';		
		!isset($data['session_prefix'])?$data['session_prefix']='':'';		
		!isset($data['session_max_life_time'])?$data['session_max_life_time']='':'';
		!isset($data['cache_server'])?$data['cache_server']='':'';		
		!isset($data['user_type'])?$data['user_type']='':'';		
		!isset($data['view_type'])?$data['view_type']='':'';		
		!isset($data['css_type'])?$data['css_type']='':'';		
		!isset($data['enabled_subdomain'])?$data['enabled_subdomain']='':'';		
		!isset($data['subdomain_suffix'])?$data['subdomain_suffix']='':'';		
		!isset($data['system_mysql_session'])?$data['system_mysql_session']='':'';		
		!isset($data['encrypt_key'])?$data['encrypt_key']='':'';	
		!isset($data['away_use_keyword'])?$data['away_use_keyword']='':'';		
		!isset($data['away_use_zhandian'])?$data['away_use_zhandian']='':'';			
		!isset($data['qq_set_data'])?$data['qq_set_data']='':'';	
		!isset($data['weblogo'])?$data['weblogo']='':'';			
			
		!isset($data['lang'])?$data['lang']='':'';	
        @include(ROOT_PATH . '/data/lang.inc');		
						
        $this->assign('data', $data);		
	    $langdata = $GLOBALS['Lang']->_get_lang_typesdata();
		$this->assign('langdata', $langdata);
					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('setting.view.html');
    }



    function set()
    {
	    $data = $_POST['data'];
        if(empty($data['site_url']))
		{
		$data['site_url'] = SITE_URL;
		}
        if(empty($data['upfile_path']))
		{
		$data['upfile_path'] = 'upfiles';
		}		
/*		if($_FILES['postfile1']['tmp_name'])
	    { 			
	    include(ROOT_PATH.'/lib/upfile.lib.php');	  
		$upfile = new Upfile('setting'); 	  
	    $updata = $upfile->_batch_upfile('setting');
		if(!empty($updata)&&isset($updata[1]['path']))
		{
		   if(defined("WATERIMG_DIR"))
		   {
              del_file(ROOT_PATH . '/' . UPFILE_PATH . '/' . WATERIMG_DIR);	
		   }
		   $data['waterimg_dir']=$updata[1]['path'];
		}
        }*/
				
		if(!isset($data['register_verycode']))$data['register_verycode']=0;
		if(!isset($data['login_verycode']))$data['login_verycode']=0;
		$temp_setdata = array();
		$TB_set = &$this->_model('setting'); 		
		if(is_file(ROOT_PATH . '/data/config_'.trim($GLOBALS['Lang']->_lang_tp).'.inc'))
		{
         include(ROOT_PATH . '/data/config_'.trim($GLOBALS['Lang']->_lang_tp).'.inc');
		 foreach($setingdata as $key => $value)
		 {
           $temp_setdata[$key] = $value;		   
		 }		 
		 
		}
		else
		{
	
		 $setingdata = $TB_set->allshow();
		 foreach($setingdata as $key => $value)
		 {
           $temp_setdata[$value['keyname']] = $value['value'];		   
		 }
			
		}

	
		foreach($data as $key => $value)
		{
           $temp_setdata[$key] = $value;	   
		}
 
		//$TB_set->replace('keyname','value',$temp_setdata);
		  
 		$this->write_config($temp_setdata);
		 
        $this->index();
     
    }

    function upfile()
    {

	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	 if(!isset($_GET["edit"]))
	 {
	 $_GET["edit"]='';
	 }	 
	 if(!isset($_GET["picurl"]))
	 {
	 $_GET["picurl"]='';
	 }
	 if(!isset($_GET["divid"]))
	 {
	 $_GET["divid"]='uppic';
	 }	 	 
	 $divid = $_GET["divid"];
	 		
		$action = $_POST["action"];
		
		$id = intval($_GET['id']);
		
		$edit = $_GET["edit"];
		$picurl = $_GET["picurl"];
        $this->assign('divid', $divid);	
				
		if($edit)
		{
        $this->assign('edit', $edit);		
		}
        if($picurl)
		{
        $this->assign('picurl', $picurl);
		}

						
		if($action == 'post')
		{
	 				
	 					if(!$_FILES['postfile1']['tmp_name']){
	 						$id || $id = intval($_POST['id']);	
							echo '<script> alert("请选择上传的图片");</script>';
						}else{
					        if(!isset($_POST["edit"]))
					        {
					         $_POST["edit"]='';
					        }	 
					        if(!isset($_POST["picurl"]))
					        {
					         $_POST["picurl"]='';
					        }
					       if(!isset($_POST['id']))
					       {
					        $_POST['id']='';
					        }	 
					       if(!isset($_POST['divid']))
					       {
					        $_POST['divid']='uppic';
					        }	
						       $divid = $_POST["divid"];												 			
						       $edit = $_POST["edit"];
						       $picurl = $_POST["picurl"];
						 	   $id || $id = intval($_POST['id']);		
				
						       include(ROOT_PATH.'/lib/upfile.lib.php');	  
					           $upfile = new Upfile('setting/'.$id); 	  
					           $updata = $upfile->_batch_upfile('setting');		 
						 		 
						       $newfile = $updata[1]['path'];
						       
							   if($edit=='editp' && $picurl)
							   {
							   
/*							          $TB_new = &$this->_model('setting');			    		 		 		
						              $TB_new->editpicurl($id,array('picurl'=>$newfile));*/
									  
						              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		               del_file($pic_dir);	
		              }					   
							   }
							   
							    if($edit=='edit' && $picurl)
						       {
						              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		               del_file($pic_dir);	
		              }				 
						       }
						 	   
							
						
						      $pic_url = SITE_URL . '/' . UPFILE_PATH . '/' .$newfile;
				
					    echo "<img src='{$pic_url}' id='upsrc' width='100px' height='100px'><br/><A HREF='?app=setting&act=upfile&edit=edit&picurl=$newfile&id=$id&divid=$divid'>修改图片</A>";
					     
						$jsfun="upfile";
					    echo "<script>
								if(self==top){
									window.opener.$jsfun('$newfile','$divid');
									window.self.close();
								}else{
									window.parent.$jsfun('$newfile','$divid');
								}
						     </script>";
								
					    exit;
						
				}
		}

        $this->assign('id', $id);
	    $this->assign('app', $this->type);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('edit',$edit);
        $this->assign('picurl',$picurl);	
			
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('upfile.view.html');
    }

		


    function write_config($data)
    {

		 $str = '';
		 $str1 = '';         
		 write_file(ROOT_PATH . '/data/lang.inc.php',"<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \r\n define('LANG','".$data['lang']."');".'?>');
		 write_file(ROOT_PATH . '/data/lang.inc',"<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \r\n ".'$'."data['lang']='".$data['lang']."';".'?>');		 
		// $lang = $data['lang'];
		 unset($data['lang']); 		 		 
         foreach ($data as $key => $value)
         {
               if (is_string($value) || is_numeric($value) || is_bool($value) || is_null($value))
               {
                    $str .= "define('".strtoupper($key)."','".$value."');\r\n";

                }
         }
		// $data['lang'] = $lang;         
         foreach ($data as $key => $value)
         {
               if (is_string($value) || is_numeric($value) || is_bool($value) || is_null($value))
               {
                    $str1 .= '$setingdata["'.$key.'"] = "'.$value."\";\r\n";

                }
         }
		 		 	    	
		 write_file(ROOT_PATH . '/data/config_'.trim($GLOBALS['Lang']->_lang_tp).'.inc.php',"<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \r\n".$str.'?>');
		 write_file(ROOT_PATH . '/data/config_'.trim($GLOBALS['Lang']->_lang_tp).'.inc',"<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \r\n".$str1.'?>');
    }


}




?>